package com.zx.webflux.controller;

import com.zx.webflux.model.User;
import com.zx.webflux.service.UserService;
import com.zx.webflux.wrapper.PageWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * <p>
 * description:  <br>
 * create: 2024-08-16 21:33
 * </p>
 *
 * @author zhou  xun
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Mono<User> getUserById(@PathVariable Long id) {
        return userService.findUserById(id);
    }

    @GetMapping("/all")
    public Flux<User> getAllUsers() {
        return userService.findAllUsers();
    }

    @PostMapping("/page")
    public Mono<PageWrapper<User>> getPageUsers(@RequestParam("pageNum") Integer pageNum,
                                                @RequestParam("pageSize") Integer pageSize) {
        return userService.getPageUsers(pageNum, pageSize);
    }

    /**
     * 分页查询
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @param ids      以逗号分隔的id字符串
     * @return {@link Flux<User>}
     * @author zhou  xun
     * @since 2024-08-16
     */
    @PostMapping("/byIds")
    public Mono<PageWrapper<User>> getUsers(@RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize,
                                            @RequestParam("ids") String ids) {

        return userService.getUsers(pageNum, pageSize, ids);
    }

}
